package Graphique;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import Connexion.ConnectionFactory;
import Connexion.ConnexionMysql;
import Connexion.ConnexionSQLLITE;
import Connexion.ConnexionSQLServer;
import StaticValues.StaticTabs;

public class CompositeConnexion extends Composite{
	
	private Combo typesConnection;
	private Label typeConLabel, hostLabel, userLabel, mdpLabel;
	private Text hostText, userText, mdpText;
	private Button accept, cancel;
	private Composite shell;
	private ConnectionFactory connection;	
	
	public CompositeConnexion(Composite shell, int style) {
		super(shell, style);
		this.shell = shell;
		this.setLayout(new GridLayout(2,false));
		
		initComposite();
	}
	
	private void initComposite(){
		
		GridData gridData = new GridData();
		gridData.horizontalAlignment = SWT.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.minimumWidth = 250;
		
		typeConLabel = new Label(this, SWT.NONE);
		typeConLabel.setText("Type de connection ");		
		
		typesConnection = new Combo(this, SWT.READ_ONLY);
		typesConnection.setItems(StaticTabs.CONNECTION);
		typesConnection.setText("Choisissez un type de connection");
		typesConnection.setLayoutData(gridData);
				
		hostLabel = new Label(this, SWT.NONE);
		hostLabel.setText("Url de connection : ");
		
		hostText = new Text(this, SWT.BORDER);	
		hostText.setText("jdbc:mysql://localhost:3306/Films");
		hostText.setLayoutData(gridData);

		userLabel = new Label(this, SWT.NONE);
		userLabel.setText("User : ");
		
		userText = new Text(this, SWT.BORDER);
		userText.setLayoutData(gridData);
		
		mdpLabel = new Label(this, SWT.NONE);
		mdpLabel.setText("Mot De passe : ");
		
		mdpText = new Text(this, SWT.BORDER | SWT.PASSWORD);
		mdpText.setLayoutData(gridData);
	
		accept = new Button(this, SWT.NONE);
		accept.setText("Valider");
		
		
		accept.addListener(SWT.Selection, new Listener() {
			
			@Override
			public void handleEvent(Event arg0) {
								
				switch (typesConnection.getSelectionIndex()){
				case	0:
					connection = new ConnexionMysql(hostText.getText(), userText.getText(), mdpText.getText(), 1);	
					break;
				case	1:
					connection = new ConnexionSQLLITE(hostText.getText(), userText.getText(), mdpText.getText(), 2);	
					break;
				case	2:
					connection = new ConnexionSQLServer(hostText.getText(), userText.getText(), mdpText.getText(), 3);	
					break;
				default : connection = new ConnexionMysql(hostText.getText(), userText.getText(), mdpText.getText(), 1);	
					break;
				}
				
				connection.connect();
				if (connection.isConnected()){
					MessageBox b= new MessageBox(getShell());
					b.open();
					shell.dispose();		
				}
				else{
					MessageBox b= new MessageBox((Shell) shell);
					b.setMessage("Vous avez du faire une erreur ! Connexion impossible");
					b.open();
				}
			}
		});
		

		cancel = new Button(this, SWT.NONE);
		cancel.setText("Annuler");
		
		cancel.addListener(SWT.Selection, new Listener() {
			
			@Override
			public void handleEvent(Event arg0) {
				shell.dispose();				
			}
		});
		
				
	}

	public boolean connecte(){
		return connection.isConnected();		
	}
	
	public ConnectionFactory getConnection(){
		return connection;
	}

}
